/*
 * @Description: 配置
 * @Author: chenju
 * @Date: 2022-01-14 14:46:49
 * @LastEditors: chenju
 * @LastEditTime: 2023-01-17 16:33:58
 */
import { reactive } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { SearchOptionsType, CPageTableProps } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'

import type { CTableProps } from '@/components/CTable'
import { getMatchRuleList } from '@/apis/system/config/factorMatchingRule'
import type { IGetFactorMatchListReq } from '@/apis/system/config/model/factorMatchingRule'
import type { BasicPagingListParams } from '@/apis/basicModel'

export const useFactorList = () => {
  // 搜索字段及其他
  const searchForm = reactive<IGetFactorMatchListReq>({
    category: '', // 规则类别
    standard: '', // 核算适用标准
    keyName: '', // 规则KEY
    factorName: '' // 因子名称
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = [
    {
      name: '规则类别',
      field: 'category',
      type: 'select',
      selectSubTitle: 'subTitle',
      fixedOption: 'MaterialsClassificationOptions'
    },
    {
      name: '适用标准',
      field: 'standard',
      type: 'select',
      fixedOption: 'StandardOptions'
    },
    {
      name: '规则KEY',
      field: 'keyName',
      type: 'input'
    },
    {
      name: '因子名称',
      field: 'factorName',
      type: 'input'
    }
  ]

  // 表格配置
  const tableOptions: CTableProps = {
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params: IGetFactorMatchListReq & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    )
    return getMatchRuleList(params)
  }

  // 处理参数
  const handleParams = (): IGetFactorMatchListReq => {
    // 参数
    let params: IGetFactorMatchListReq = {
      category: searchForm.category,
      standard: searchForm.standard,
      keyName: searchForm.keyName,
      factorName: searchForm.factorName
    }
    params = removeUndefinedParams(params)
    console.log('handleParams', params)
    return params
  }

  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: '规则类别',
      field: 'category',
      width: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '适用标准',
      field: 'applicableStandard',
      slot: 'applicableStandard',
      minWidth: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '规则KEY',
      field: 'keyName',
      minWidth: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '规则因子',
      field: 'factorName',
      slot: 'factorName',
      minWidth: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建人/创建时间',
      field: 'updateName',
      slot: 'updateInfo',
      width: '170',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 110,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
